/*
 * @name accountSettings.js

* @copyright (c) 2011. You are free to use this class.
 * 				  Modify it the way you want it.
 * 				  Please let me know about any modifications done.
*
*/
var UserAccount = {
       
       /* Function init
        * This function is the entry point of the Object.
        * @param void
        * @return void
        * @access public
        * */
       init:function(){
            
             /* Handle account settings action */
    		$("a#account-setting").bind('click',function(event){
    			event.preventDefault(); //Preventing browser default functionalities
                $.colorbox({
                    inline:true,
                    scrolling:false,
                    overlayClose:false,
                    escKey:false,
                    top:'10%',
                    title:'Account Information',
                    href:"#account-form-window",
                });
    		});
            
             /* Handle the change password checkbox */
            $('#reset-pass-check').click(function(){
                  // check its status and display the change password box.
                if($('#reset-password-holder').is(':visible')) {
                    $.validationEngine.closePrompt('.formError',true);
                    $("#old_password").val('');
                    $("#password_1").val('');
                    $("#password_2").val('');
                    $('#reset-password-holder').hide();
                } else {
                  $('#reset-password-holder').show();
                }
                $.colorbox.resize()
            });
            
            $('#account-save-btn').click(function(){
                 UserAccount.saveSettings();
            });
            
            $('#account-cancel-btn').click(function(){
                 document.getElementById('account-form').reset();
                 $.validationEngine.closePrompt('.formError',true);
                 $.colorbox.close();
            });
            
            jLib.afterClosePopUpForm('account-form');
            
            $('#account-form').validationEngine();
      
       },

       /* Function used to save the data sent by the user
        * @name saveData
        * @param formId
        * @return void
        * @access public
        *  */
       saveSettings:function(){
          /* Validate the form*/
          var validationStatus =  $('#account-form').validationEngine({returnIsValid:true});
           /* When the Validation Status is true meaning that the data input are correct then the data can be processed. */
          if(validationStatus){
            /*If the checkbox is checked, then all passwords fields must not be empty*/
			
			if($('#reset-pass-check').attr('checked')){//Validate and Build promt
				if($("#old_password").val() =="" ||  $("#password_1").val() == "" ||  $("#password_2").val() == ""){
    				$.validationEngine.buildPrompt("#reset-password-holder","All passwords fields are required","error");
                    return false;
    			}
			}
			
			 /* Preparation of the data to send to the server to be processed. Here we retrieve the data from the login form. */
             var query = $('#account-form').serialize();
             //var query = $("#account-form .username").serialize() 
             /* Send the data to the server and handle the server response */
             $.post('Users/changeLogin', query, function(response){
                var txt = '';
                if(typeof response.mesg  == 'object'){
					for(megTxt in response.mesg){
						txt += response.mesg[megTxt]+'<br />';
					}
				}
				else{
					txt = response.mesg
				}
                
				if(response.code === 0){
					document.getElementById('account-form').reset();
					$.validationEngine.closePrompt('.formError',true);
                    
                    $('#reset-pass-check').attr('checked','');
                    $('#reset-password-holder').hide();
                    
                    $.colorbox.close();
				    //jLib.setFeedback(txt, 'success_message', false);
                    jAlert(txt+"<br /> Logging Out for changes to take effect",'Alert',function(){
                        window.location = 'users/logout';
                    });
				}
				/* When there are Errors */
				else if(response.code === 1){
					$.validationEngine.buildPrompt(".account-settings-prompt",txt,"error");
					 //jLib.setFeedback(response.mesg, 'error_message', false);
				}
				else if(response.code === 2){
					$.validationEngine.buildPrompt(".account-settings-prompt",txt,"error");
					 //jLib.setFeedback(response.mesg, 'error_message', false);
				}
				else if(response.code === 3){
					$.validationEngine.buildPrompt(".account-settings-prompt",txt,"error");
					// jLib.setFeedback(response.mesg, 'error_message', false);
				}
				else{
					$.validationEngine.buildPrompt(".account-settings-prompt",txt,"error");
					// jLib.setFeedback(response.mesg, 'error_message', false);
				}
            },'json');
          }
       },


	   /* This function creates a modal window dialog
        * @param string entity
        * @return void
        * @access public
        * */
       createDialog:function(){
              // dialog properties
              var dialogTitle = null;
              var dialogButtons = {
                     'save changes':function(){
                            $thisObj.saveSettings();
                     },
                     'close':function(){
						$.validationEngine.closePrompt('.formError',true)
                          $('#account-form-window').dialog('close') ;
                     }
              };

              dialogTitle = 'User Account Details';
              // render the dialog box
             $('#account-form-window').dialog({
                modal: true,
                resizable: false,
                title: dialogTitle,
                width: 400,
                buttons: dialogButtons,
				open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
				
              });
       }  
       
};

/* when the page is loaded */
$(document).ready(function() {
	//Hiding the loader bar
       UserAccount.init();
});

